<template>
	<view>
		<view class="head">
			<block v-if="hasLogin">
				<view class="userinfo" @click="toUcenter">
					<u-avatar :customStyle="{marginRight:'20rpx'}" :src="userInfo.avatar"></u-avatar>
					<view class="username">
						<text>{{ userInfo.nickname }}</text>
						<view class="sub-txt">
							<text>等级：{{ userInfo.level }}</text>
							<view @click.stop="jumpVip" v-if="userInfo.is_vip" url="vip" class="vip-tips">vip会员</view>
						</view>
					</view>
					<view class="arrow-right">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="btn-login">
					<q-button @click="toLogin" plain>登录</q-button>
				</view>
			</block>
			<u-grid v-if="userInfo" :col="4" :border="false" style="margin: 20rpx 0;" @click="toNav">
				<u-grid-item name="/pages/my/fans">
					<text>{{ userInfo.fans_num || 0 }}</text>
					<view class="grid-text">粉丝</view>
				</u-grid-item>
				<u-grid-item name="/pages/my/follow">
					<text>{{ userInfo.follow_num || 0 }}</text>
					<view class="grid-text">关注</view>
				</u-grid-item>
				<u-grid-item name="/pages/my/post">
					<text>{{ userInfo.post_num || 0 }}</text>
					<view class="grid-text">帖子</view>
				</u-grid-item>
				<u-grid-item>
					<text>{{ userInfo.integral || 0 }}</text>
					<view class="grid-text">积分</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- 社区服务 -->
		<view class="block-wrap">
			<view class="block-title">社区服务</view>
			<u-grid :col="4" :border="false" style="margin: 20rpx 0;" @click="toNav">
				<u-grid-item name="/pages/my/post">
					<image class="gn-icon" src="/static/m_3.png"></image>
					<view class="grid-text">我的帖子</view>
				</u-grid-item>

				<u-grid-item name="/pages/my/thumb">
					<image class="gn-icon" src="/static/m_4.png"></image>
					<view class="grid-text">点赞的帖子</view>
				</u-grid-item>

				<u-grid-item name="/pages/topic/add">
					<image class="gn-icon" src="/static/m_6.png"></image>
					<view class="grid-text">创建圈子</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- 帮助 -->
		<view class="block-wrap">
			<view class="block-title">帮助</view>
			<view class="grid-wrap">
				<view class="grid-item">
					<button open-type="share" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_1.png"></image>
						<view class="grid-text">发给好友</view>
					</button>
				</view>
				<view class="grid-item">
					<view class="grid-item" @click="toNav('/pages/topic/qrcode?id='+currentTopicId)">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_2.png"></image>
						<view class="grid-text">分享二维码</view>
					</view>
				</view>
				<view class="grid-item">
					<button open-type="contact" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_8.png"></image>
						<view class="grid-text">帮助与反馈</view>
					</button>
				</view>
				<view class="grid-item">
					<button @click="onClear" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_9.png"></image>
						<view class="grid-text">清除缓存</view>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		computed: {
			hasLogin() {
				return this.$store.state.hasLogin;
			},
			userInfo() {
				return this.$store.state.userInfo;
			},
			msgCount() {
				return this.$store.state.messegeNum;
			},
			currentTopicId() {
				return this.$store.state.currentTopicId;
			},
			currentTopic() {
				return this.$store.state.currentTopic;
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {
				// 来自页面内分享按钮
				console.log(res.target);
			}
			return {
				title: this.currentTopic.name + '-' + this.currentTopic.description,
				path: '/pages/index/index?topic_id=' + this.currentTopicId,
				imageUrl: this.currentTopic.bg_image
			};
		},
		onShareTimeline() {
			return {
				title: this.currentTopic.name + '-' + this.currentTopic.description,
				imageUrl: this.currentTopic.bg_image,
				query: 'topic_id=' + this.currentTopicId
			};
		},
		onShow() {
			if (this.userInfo && this.userInfo.id) {
				this.getMsgNum();
				this.getUserInfo();
			} else {
				this.$store.commit("logout")
			}
		},
		onLoad() {},
		methods: {
			onClear() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否清除登录信息以及数据缓存?',
					success: function(res) {
						if (res.confirm) {
							uni.clearStorageSync();
							that.$store.commit("logout");
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getMsgNum() {
				this.$H.post('message/num').then(res => {
					let messageNum = res.result.all_count
					if (messageNum > 0) {
						uni.setTabBarBadge({
							index: 1,
							text: messageNum + ''
						})
					} else {
						uni.removeTabBarBadge({
							index: 1
						})
					}
				});
			},
			getUserInfo() {
				this.$H.post('user/info').then(res => {
					let userInfo = res.result;
					userInfo.token = this.userInfo.token;
					this.$store.commit("login", userInfo);
				});
			},
			jumpVip() {
				uni.navigateTo({
					url: 'vip'
				});
			},
			toLogin() {
				uni.navigateTo({
					url: '/pages/user/login'
				});
			},
			toUcenter() {
				uni.navigateTo({
					url: '/pages/user/edit-info/edit'
				});
			},
			toNav(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.head {
		padding: 20rpx;
		background-color: #fff;

		.sub-txt {
			font-size: 24rpx;
			color: #616161;

			.vip-tips {
				display: inline-block;
				background-image: linear-gradient(to right, #f4e4cb, #e9caad, #dfb492);
				color: #fff;
				padding: 0rpx 10rpx;
				border-radius: 100rpx;
				margin-left: 10rpx;
			}
		}

		margin-bottom: 20rpx;
	}

	.userinfo {
		display: flex;
		align-items: center;
		padding: 20rpx;

		.arrow-right {
			margin-left: auto;
		}
	}

	.userinfo .username {
		display: flex;
		flex-direction: column;
	}

	.grid-text {
		color: #999;
		font-size: 12px;
		margin-bottom: 20rpx;
	}


	.btn-login {
		margin: 40rpx 0;
	}

	.gn-icon {
		width: 60rpx;
		height: 60rpx;
		margin-bottom: 20rpx;
	}

	/*服务按钮*/
	.btn-wrap {
		display: flex;
		margin-top: 30rpx;
	}

	.btn-wrap .btn-contact {
		background-color: #fff;
		margin-left: 15rpx;
		margin-right: 15rpx;
		padding: 20rpx;
		line-height: unset;
		font-size: 12px;
		color: #999;
	}

	.btn-wrap .btn-contact:active {
		background-color: #f5f5f5;
	}

	.btn-wrap .btn-contact .txt {
		margin-top: 20rpx;
	}

	.btn-wrap .btn-contact::after {
		border: unset;
		position: unset;
	}

	.icon-size {
		font-size: 50rpx;
	}

	.block-wrap {
		background-color: #fff;
		border-radius: 20rpx;
		margin: 20rpx;
		overflow: hidden;

		.block-title {
			background-color: #fff;
			padding: 20rpx;
		}
	}

	.grid-wrap {
		display: grid;
		grid-template-columns: repeat(4, 1fr);

		.grid-item {
			text-align: center;
			position: relative;

			.grid-text {
				color: #999;
				font-size: 12px;
				margin-bottom: 20rpx;
			}

			.message-num {
				position: absolute;
				right: 20rpx;
				top: 0;
				padding: 5rpx;
				width: 35rpx;
				height: 35rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50%;
				background-color: red;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}
</style>